from django.contrib import admin

# Register your models here.
from .models import Author, Genre, Book, BookInstance, BookRating, BookReview, BookLike, BookFavorite, FavoriteGroup, ReviewLike, ReviewFavorite, ReviewComment

# admin.site.register(Book)
# admin.site.register(Author)
admin.site.register(Genre)
# admin.site.register(BookInstance)

# Register the Admin classes for Book using the decorator

class BooksInstanceInline(admin.TabularInline):
    list_dis = ('title', 'author', 'status', 'date_of_death', 'uuid')
    model = BookInstance

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'author', 'display_genre')
    inlines = [BooksInstanceInline]


# Register the Admin classes for BookInstance using the decorator

@admin.register(BookInstance)
class BookInstanceAdmin(admin.ModelAdmin):
    list_display = ('book', 'status', 'display_borrower', 'due_back', 'id')
    list_filter = ('status', 'due_back')

    fieldsets = (
        (None, {
            'fields': ('book','imprint', 'id')
        }),
        ('Availability', {
            'fields': ('status', 'due_back', 'borrower')
        }),
    )

    def display_borrower(self, obj):
        return obj.borrower.username if obj.borrower else ''
    display_borrower.short_description = 'Borrower'


# Define the admin class
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('last_name', 'first_name', 'date_of_birth', 'date_of_death')
    fields = ['first_name', 'last_name', ('date_of_birth', 'date_of_death')]

# Register the admin class with the associated model
admin.site.register(Author, AuthorAdmin)

@admin.register(ReviewLike)
class ReviewLikeAdmin(admin.ModelAdmin):
    list_display = ('review', 'user', 'created_at')
    list_filter = ('created_at', 'user', 'review__book__title')
    search_fields = ('user__username', 'review__content')

@admin.register(ReviewFavorite)
class ReviewFavoriteAdmin(admin.ModelAdmin):
    list_display = ('review', 'user', 'created_at')
    list_filter = ('created_at', 'user', 'review__book__title')
    search_fields = ('user__username', 'review__content')

@admin.register(ReviewComment)
class ReviewCommentAdmin(admin.ModelAdmin):
    list_display = ('review', 'user', 'content', 'created_at', 'parent_comment')
    list_filter = ('created_at', 'user', 'review__book__title')
    search_fields = ('content', 'user__username', 'review__content')
    raw_id_fields = ('parent_comment',)
